package com.fish.book.ui;

import com.fish.book.PersistentState;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SettingUi {

//
//    public static void main(String[] args) {
//        JFrame frame = new JFrame("SettingUi");
//        frame.setContentPane(new SettingUi().mainPanel);
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.pack();
//        frame.setVisible(true);
//    }


    public JPanel mainPanel;
    public JLabel chooseFileLabel;
    public JLabel label1;
    public JButton button2;
    public JCheckBox showFlag;
    public JLabel label2;
    public JLabel Label3;
    public JComboBox autoNextSecond;
    public JLabel lable5;
    public JLabel lable4;
    public JTextField before;
    public JTextField next;
    public JTextField bookPathText;
    public JLabel l;
    public JLabel autoKey;
    public JTextField autoKeymap;


    public SettingUi() {
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.showOpenDialog(mainPanel);
                File file = fileChooser.getSelectedFile();
                if (bookPathText != null) {
                    bookPathText.setText( file.getPath());
                }
            }
        });
    }


    public void innit(PersistentState persistentState) {
        if (autoNextSecond.getSelectedItem() == null) {
            autoNextSecond.setSelectedItem("5");
        }
        bookPathText.setText(persistentState.getBookPathText());
        showFlag.setSelected("1".equals(persistentState.getShowFlag()));
        autoNextSecond.setSelectedItem(persistentState.getAutoNextSecond());
        before.setText(persistentState.getBefore());
        next.setText(persistentState.getNext());
        autoKeymap.setText(persistentState.getAutoKeymap());


        autoKeymap.setEditable(false);
        before.setEditable(false);
        next.setEditable(false);
        bookPathText.setEditable(false);


    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new FormLayout("fill:max(d;4px):noGrow,left:13dlu:noGrow,left:46dlu:noGrow,fill:203px:noGrow,fill:51px:noGrow,fill:max(d;4px):noGrow,fill:max(d;4px):noGrow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        chooseFileLabel = new JLabel();
        chooseFileLabel.setHorizontalAlignment(11);
        chooseFileLabel.setText("选择文件:");
        CellConstraints cc = new CellConstraints();
        mainPanel.add(chooseFileLabel, cc.xy(3, 3, CellConstraints.LEFT, CellConstraints.FILL));
        label1 = new JLabel();
        label1.setText("");
        mainPanel.add(label1, cc.xy(1, 3));
        button2 = new JButton();
        button2.setText("...");
        mainPanel.add(button2, cc.xy(5, 3, CellConstraints.CENTER, CellConstraints.CENTER));
        label2 = new JLabel();
        label2.setText("精简模式:");
        mainPanel.add(label2, cc.xy(3, 5, CellConstraints.LEFT, CellConstraints.DEFAULT));
        Label3 = new JLabel();
        Label3.setText("自动翻页(秒):");
        mainPanel.add(Label3, cc.xy(3, 7));
        autoNextSecond = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("1");
        defaultComboBoxModel1.addElement("2");
        defaultComboBoxModel1.addElement("3");
        defaultComboBoxModel1.addElement("4");
        defaultComboBoxModel1.addElement("5");
        defaultComboBoxModel1.addElement("6");
        defaultComboBoxModel1.addElement("7");
        defaultComboBoxModel1.addElement("8");
        defaultComboBoxModel1.addElement("9");
        defaultComboBoxModel1.addElement("10");
        autoNextSecond.setModel(defaultComboBoxModel1);
        autoNextSecond.setToolTipText("");
        mainPanel.add(autoNextSecond, cc.xy(4, 7));
        lable4 = new JLabel();
        lable4.setText("上一页热键:");
        mainPanel.add(lable4, cc.xy(3, 11));
        lable5 = new JLabel();
        lable5.setText("下一页热键:");
        mainPanel.add(lable5, cc.xy(3, 13));
        before = new JTextField();
        before.setText("");
        mainPanel.add(before, cc.xy(4, 11, CellConstraints.FILL, CellConstraints.DEFAULT));
        next = new JTextField();
        mainPanel.add(next, cc.xy(4, 13, CellConstraints.FILL, CellConstraints.DEFAULT));
        bookPathText = new JTextField();
        mainPanel.add(bookPathText, cc.xy(4, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        l = new JLabel();
        l.setText("");
        mainPanel.add(l, cc.xy(2, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        showFlag = new JCheckBox();
        showFlag.setText("");
        mainPanel.add(showFlag, cc.xy(4, 5, CellConstraints.LEFT, CellConstraints.DEFAULT));
        autoKey = new JLabel();
        autoKey.setText("自动翻页热键:");
        mainPanel.add(autoKey, cc.xy(3, 9));
        autoKeymap = new JTextField();
        autoKeymap.setText("");
        mainPanel.add(autoKeymap, cc.xy(4, 9, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }
}
